package client.ui;

import java.util.List;

import phasebook.client.msgs.UserInfo;
import client.Controller;
import client.Controller.ResponseHandler;
import client.api.CombinedResponse;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.HTMLPane;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class UserFinder extends Window implements ResponseHandler{
	private VLayout layout = new VLayout();
	private Controller controller;

	public UserFinder(Controller controller) {
		this.controller = controller;
		super.setWidth(300);
		super.setHeight(500);
		super.setTitle("Invite new Friends");
		super.setShowMinimizeButton(false);
		super.setIsModal(true);
		super.setShowModalMask(true);
		super.centerInPage();

		final TextItem findValueItem = new TextItem();
		findValueItem.setWidth(180);
		findValueItem.setHeight(25);
		findValueItem.setTitle("Name");
		findValueItem.setValue("");

		IButton findButton = new IButton("Find");
		findButton.setWidth100();
		findButton.setHeight(25);
		findButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				UserFinder.this.controller.findUsers(findValueItem.getValueAsString());
			}
		});
		
		DynamicForm findForm = new DynamicForm();
		findForm.setWidth100();
		findForm.setHeight(25);
		findForm.setNumCols(2);
		findForm.setFields(findValueItem);
		
		HLayout findTool = new HLayout();
		findTool.setHeight("1%");
		findTool.addMember(findForm);
		findTool.addMember(findButton);
		 
		
		layout.setHeight("25%");
		layout.setOverflow(Overflow.AUTO);
		super.addItem(findTool);
		super.addItem(layout);
		controller.addResponseHandler(this);

	}
	
	private IButton createUserButton(final UserInfo info){
		IButton userButton = new IButton(info.getName());
		userButton.setWidth100();
		userButton.setHeight(25);
		userButton.setAlign(Alignment.LEFT);
		userButton.setIcon("[SKIN]/headerIcons/person.png");
		userButton.setOverflow(Overflow.VISIBLE);
		userButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				controller.requestFriendship(info.getUserId());
			}
		});
		
		return userButton;
	}

	@Override
	public void onResponse(CombinedResponse response) {
		List<UserInfo> foundUsers = response.getFoundUsers();
		if (foundUsers!=null){
			Canvas[] items = new Canvas[foundUsers.size()];
			for(int i=0;i<foundUsers.size();i++)
				items[i] = createUserButton(foundUsers.get(i));
			
			layout.setMembers(items);
			HTMLPane emptySpace = new HTMLPane();
			emptySpace.setWidth100();
			emptySpace.setHeight100();
			emptySpace.setOverflow(Overflow.CLIP_V);
			layout.addMember(emptySpace);
		}
	}
}
